include ../Common.mk

MODULES  = $(shell ls -d */)
OBJS     = $(shell ls -d ./*/*.o ./*/*/*.o)

# 在这里定义 KERNEL_ELF 和 KERNEL_LD
KERNEL_ELF = ../kernel-qemu

ifeq ($(PLATFORM), qemu)
KERNEL_LD  = ../linker/kernel.ld
else
KERNEL_LD  = ../linker/machine.ld
endif

.PHONY: build clean $(MODULES) 

$(MODULES):
	$(MAKE) build --directory=$@

$(KERNEL_ELF): $(MODULES) $(KERNEL_LD)
	$(LD) $(LDFLAGS) -T $(KERNEL_LD) -o $(KERNEL_ELF) $(OBJS)

build: $(MODULES) $(KERNEL_ELF)

clean:
	for d in $(MODULES); \
		do \
			$(MAKE) clean --directory=$$d; \
		done
	rm -f ${KERNEL_ELF}